﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1065010_1065053_1065059_1065064
{
    public partial class DSKhachHang : Form
    {
        public DSKhachHang()
        {
            InitializeComponent();
        }
        string chuoiload = "select * from KhachHang";
        private void btnclose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void DSKhachHang_Load(object sender, EventArgs e)
        {
            dgvdskh.DataSource = DataProvider.HienThiDanhSach(chuoiload);
        }

        private void btninsert_Click(object sender, EventArgs e)
        {
            SqlConnection kn = DataProvider.ConnectionData();
            string sql = "Insert Into KhachHang(MaKH, TenKH, DiaChi, DienThoai, Email) Values (@MaKH, @TenKH, @DiaChi, @DienThoai, @Email)";
            SqlCommand lenh = new SqlCommand(sql, kn);

            KhachHang kh = new KhachHang();
            kh.makh = txtmakh.Text;
            kh.tenkh = txttenkh.Text;
            kh.diachi = txtdiachi.Text;
            kh.dienthoai = txtdienthoai.Text;
            kh.email = txtemail.Text;
         
            SqlParameter para1 = new SqlParameter("@MaKH", kh.makh);
            lenh.Parameters.Add(para1);
            SqlParameter para2 = new SqlParameter("@TenKH", kh.tenkh);
            lenh.Parameters.Add(para2);
            SqlParameter para3 = new SqlParameter("@DiaChi", kh.diachi);
            lenh.Parameters.Add(para3);
            SqlParameter para4 = new SqlParameter("@DienThoai", kh.dienthoai);
            lenh.Parameters.Add(para4);
            SqlParameter para5 = new SqlParameter("@Email", kh.email);
            lenh.Parameters.Add(para5);
            lenh.ExecuteNonQuery();
            dgvdskh.DataSource = DataProvider.HienThiDanhSach(chuoiload);
            kn.Close();
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            string makh = dgvdskh.CurrentRow.Cells[0].Value.ToString();
            SqlConnection kn = DataProvider.ConnectionData();
            string sql = "delete from KhachHang where MaKH=@MaKH";
            SqlCommand lenh = new SqlCommand(sql, kn);
            SqlParameter para = new SqlParameter("MaKH", SqlDbType.VarChar, 10);
            para.Direction = ParameterDirection.Input;
            para.Value = makh;
            lenh.Parameters.Add(para);
            lenh.ExecuteNonQuery();
            dgvdskh.Rows.Remove(dgvdskh.CurrentRow);
            dgvdskh.Refresh();
            dgvdskh.DataSource = DataProvider.HienThiDanhSach(chuoiload);
            kn.Close();
        }


    }
}
